package com.ruoyi.advertising.listener;

import com.rabbitmq.client.Channel;
import com.ruoyi.advertising.domain.GroupPurchase;
import com.ruoyi.advertising.service.IGroupPurchaseService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderListener {
    @Autowired
    private IGroupPurchaseService groupPurchaseService;

    // 重点：一定要监听的是死信队列!!!
    @RabbitListener(queues = "advertising_dlx_queue")
    public void listenerMessage(Message message, Channel channel) throws IOException {
        try {
            String strId = new String(message.getBody());
            Long id = Long.valueOf(strId);
            System.out.println(id);
            GroupPurchase groupPurchase = new GroupPurchase();
            groupPurchase.setId(id);
            groupPurchase.setDeleted(1L);
            groupPurchaseService.updateGroupPurchase(groupPurchase);
            /*
             * 3、无异常，手动签收：
             * */
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            /*
             * 4、出现异常，拒绝签收，重回队列
             * */
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, true);
        }
    }
}